package tuiLayer;

import controlLayer.PersonCtr;

import modelLayer.Person;

import java.util.Scanner;
/**
 * Write a description of class PersonUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonUI
{
    // instance variables - replace the example below with your own
    private PersonCtr personCtr;
    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI()
    {
        // initialise instance variables
        personCtr = new PersonCtr();
    }

    public void createPerson(String cpr, String name, String address, String postalCode, String city, String phone)
    {
        personCtr.createPerson(cpr,name,address,postalCode,city,phone);
    }

    public Person findPerson(String cpr)
    {
        return personCtr.findPerson(cpr);
    }

    public void updatePerson(Person person, String cpr, String name, String address, String postalCode, String city, String phone)
    {
        personCtr.updatePerson(person,cpr,name,address,postalCode,city,phone);
    }

    public void deletePerson(Person person)
    {
        personCtr.deletePerson(person);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Friends menu loop
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeFriendsMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = friendsAddStart();
            }
            else if(choice == 2)
            {
                print = friendsFindStart();
            }
            else if(choice == 3)
            {
                print = friendsUpdateStart();
            }
            else if(choice == 4)
            {
                print = friendsRemoveStart();

            }
            else if(choice == 5) 
            {
                exit = true;
            }

        }
    }

    // Write friends menu
    public void writeFriendsMenu(String print) 
    {
        System.out.println("\f *** Friends ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find");
        System.out.println(" (3)Update");
        System.out.println(" (4)Remove");
        System.out.println(" (5)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a friend"
    public String friendsAddStart() 
    {
        System.out.println("\nEnter details for the new Friend: ");
        System.out.println("Format: <cpr> <name> <address> <postal code> <city> <phone>");

        String print = null;

        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] inputWords = input.split(" ");
        if(inputWords.length == 6) 
        {
            String cpr = inputWords[0];
            String name = inputWords[1];
            String address = inputWords[2];
            String postalCode = inputWords[3];
            String city = inputWords[4];
            String phone = inputWords[5];

            createPerson(cpr,name,address,postalCode,city,phone);

            print = "Person #" + cpr + " has been added to your Friends.";
        }
        else 
        {
            print = "ERROR: Bad input.";
        }

        return print;
    }

    // Start "Find a friend"
    public String friendsFindStart() 
    {
        String print = null;

        System.out.print("\nEnter CPR: ");

        Scanner sc = new Scanner(System.in);
        String cpr = sc.next();

        Person foundPerson = findPerson(cpr);
        if(foundPerson != null) 
        {
            print = " # Found Person: #" + "\n" 
            + "  CPR: " + foundPerson.getCpr() + "\n"
            + "  Name: " + foundPerson.getName() + "\n"
            + "  Address: " + foundPerson.getAddress() + "\n"
            + "  Postal code: " + foundPerson.getPostalCode() + "\n"
            + "  City: " + foundPerson.getCity() + "\n"
            + "  Phone: " + foundPerson.getPhone() + "\n";
        }
        else 
        {
            print = "ERROR: Person not found.";
        }
        return print;
    }

    // Start "Update a friend"
    public String friendsUpdateStart() 
    {
        String print = null;
        System.out.print("\nEnter CPR:");

        Scanner sc = new Scanner(System.in);
        String cpr = sc.next();

        Person foundPerson = findPerson(cpr);
        if(foundPerson != null) 
        {
            System.out.println("\nEnter the new details for Friend #" + cpr + " :");
            System.out.println("Format: <cpr> <name> <address> <postal code> <city> <phone>");
            
            Scanner newSc = new Scanner(System.in);
            String input = newSc.nextLine();
            String[] inputWords = input.split(" ");
            if(inputWords.length == 6) 
            {
                String newCpr = inputWords[0];
                String newName = inputWords[1];
                String newAddress = inputWords[2];
                String newPostalCode = inputWords[3];
                String newCity = inputWords[4];
                String newPhone = inputWords[5];

                updatePerson(foundPerson,newCpr,newName,newAddress,newPostalCode,newCity,newPhone);

                print = "Person #" + newCpr + " has been updated to:\n" + getPersonInfo(foundPerson);
            }
            else 
            {
                print = "ERROR: Bad input.";
            }
        }
        else 
        {
            print = "ERROR: Bad input.";
        }
        return print;
    }
    
    private String getPersonInfo(Person person) 
    {
        String string = "  CPR: " + person.getCpr() + "\n"
            + "  Name: " + person.getName() + "\n"
            + "  Address: " + person.getAddress() + "\n"
            + "  Postal code: " + person.getPostalCode() + "\n"
            + "  City: " + person.getCity() + "\n"
            + "  Phone: " + person.getPhone() + "\n";
        return string;
    }
    
    // Start "Remove a friend"
    public String friendsRemoveStart() 
    {
        String print = null;
        
        System.out.print("\nEnter cpr: ");

        Scanner sc = new Scanner(System.in);
        String cpr = sc.next();

        Person foundPerson = findPerson(cpr);
        
        if(foundPerson != null) 
        {
            deletePerson(foundPerson);
            print = "Friend #" + cpr + " has been deleted.";
        }
        else 
        {
            print = "ERROR: Person not found.";
        }
        return print;
    }
}

    
    
    
    
    
    
    
    